package cn.com.backstage.controller;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiArticle;
import cn.com.api.entity.ApiT_city;
import cn.com.api.service.ApiArticleService;
import cn.com.util.ImageUtils;
import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE)
public class ArticleController {

	private static Logger logger = Logger.getLogger(ArticleController.class);

	@Autowired
	private ApiArticleService apiArticleService;

	/**
	 * 进入查询界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "article-list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/article_list.jsp");

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		list = apiArticleService.searAll();
		mv.addObject("articles", list);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "article-add-pre")
	public ModelAndView article_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/article_add.jsp");

		String id = request.getParameter("id");
		if (id != null) {
			Map<String, Object> article = new HashMap<String, Object>();
			article = apiArticleService.searById(id);
			mv.addObject("article", article);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "article-add")
	public void article_add(ApiArticle article, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String title = request.getParameter("title");
		String description = request.getParameter("description");
		String url = request.getParameter("url");
		String grade = request.getParameter("grade");
		String enable = request.getParameter("enable");
		String image = request.getParameter("image");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiArticle art = new ApiArticle();
			art.setTitle(title);
			art.setDescription(description);
			art.setUrl(url);
			art.setEnable(Integer.valueOf(enable));
			art.setGrade(Integer.valueOf(grade));
			art.setImage(image);

			apiArticleService.add(art);
			map.put(Utils.MESSAGE, "添加成功！");
		}
		// edit
		else {
			apiArticleService.update(article);
			map.put(Utils.MESSAGE, "编辑成功！");
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "article-delete")
	public void article_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		if (apiArticleService.delete(id)) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 上传文件
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "article-pics-upload")
	public void juan_pics_upload(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String path = ImageUtils.getServletWebAppsPath(request) + "/article/";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String sdfStr = sdf.format(new Date());
		String fileName = sdfStr + "_" + file.getOriginalFilename();

		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		String logo_path = "";
		Map<String, Object> map = new HashMap<String, Object>();
		// 保存
		try {
			file.transferTo(targetFile);
			logo_path = ImageUtils.getDataBaseImagePath() + "/article/" + fileName;

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "上传成功！");
			map.put("datas", logo_path);

		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		//
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
